package com.nxxskj.alcohol.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nxxskj.alcohol.bean.Dept;
import com.nxxskj.alcohol.bean.Role;
import com.nxxskj.alcohol.bean.User;
import com.nxxskj.alcohol.service.DeptService;
import com.nxxskj.alcohol.service.RecordService;
import com.nxxskj.alcohol.service.RoleService;
import com.nxxskj.alcohol.service.UserService;
import com.nxxskj.common.model.RestResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private DeptService deptService;

    @Autowired
    private RecordService recordService;

    @RequestMapping("/getUserInfo.do")
    public String getUserInfo(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        request.setAttribute("user", user);
        return "userinfo";
    }


    @RequestMapping("/getAllUsers.do")
    public String getAllUsers(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        List<User> users = userService.getallUsersByUser(user);
        request.setAttribute("users", users);
        return "users";
    }

    @RequestMapping("/editUser.do")
    public String editUser(HttpServletRequest request) {
        String action = request.getParameter("action");
        User user = (User) request.getSession().getAttribute("user");

        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(user.getRole())) {
            roleWrapper.eq(Role::getReportTo, user.getRole());
        }

        roleWrapper.orderByAsc(Role::getDept);
        List<Role> roleList = roleService.list(roleWrapper);

        Map<String, String> map = new LinkedHashMap<>();
        for (Role role : roleList) {
            map.put(role.getName(), role.getName());
        }

        LambdaQueryWrapper<Dept> deptWrapper = new LambdaQueryWrapper<>();
        deptWrapper.likeRight(Dept::getDeptCode, user.getDeptCode());
        List<Dept> deptList = deptService.list(deptWrapper);
        User addUser = new User();
        String uid = request.getParameter("id");
        if (StringUtils.isNotBlank(uid)) {
            addUser = userService.getById(uid);
        }
        request.setAttribute("addUser", addUser);
        request.setAttribute("deptList", deptList);
        request.setAttribute("action", action);
        request.setAttribute("roleList", map);
        return "editUser";
    }

    @ResponseBody
    @RequestMapping("/savaUser.do")
    public RestResponse saveUser(User user) {
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dept::getDeptCode, user.getDeptCode());
        Dept dept = deptService.getOne(wrapper);
        user.setDeptName(dept.getDeptName());


        LambdaQueryWrapper<User> userWrapper=new LambdaQueryWrapper<>();
        userWrapper.eq(User::getUserName,user.getUserName());
        userWrapper.or().eq(User::getPhone,user.getPhone());
        int count=userService.count(userWrapper);
        boolean flag=false;

        if(StringUtils.isNotBlank(user.getId()))
        {
            if(count<=1)
            {
                flag=userService.updateById(user);

            }else{
                return RestResponse.error(500,"用户名、手机号不能和系统中的重复");
            }

        }else{

            if(count==0)
            {
                flag=userService.save(user);
            }else{
                return RestResponse.error(500,"用户名、手机号不能和系统中的重复");
            }
        }
        if (flag) {
            return RestResponse.success();
        } else {
            return RestResponse.error();
        }
    }

    @ResponseBody
    @RequestMapping("/delUser.do")
    public RestResponse delUser(String uid) {
        boolean flag = userService.removeById(uid);
        if (flag) {
            return RestResponse.success();
        } else {
            return RestResponse.error();
        }
    }
}
